<template>
  <div class="manage">
    <el-row>
      <el-button type="primary" @click="addEvt">添加</el-button>
    </el-row>
    <el-table
      :data="
        tableData.filter(
          (data) =>
            !search || data.name.toLowerCase().includes(search.toLowerCase())
        )
      "
      style="width: 100%"
    >
      <el-table-column label="年龄" prop="date"> </el-table-column>
      <el-table-column label="姓名" prop="name"> </el-table-column>
      <el-table-column align="right">
        <template slot="header" slot-scope="scope">
          <el-input v-model="search" size="mini" placeholder="输入关键字搜索" />
        </template>
        <template slot-scope="scope">
          <el-button size="mini" @click="handleEdit(scope.$index, scope.row)"
            >Edit</el-button
          >
          <el-button
            size="mini"
            type="danger"
            @click="handleDelete(scope.$index, scope.row)"
            >Delete</el-button
          >
        </template>
      </el-table-column>
    </el-table>
    <el-button type="text" @click="dialogFormVisible = true"></el-button>
    <AddInfo />
    <EditInfo />

  </div>
  
</template>

<script>
import axios from "axios";
import AddInfo from "./addInfo";
import EditInfo from './edit';


export default {
  data() {
    return {
      tableData: [],
      search: "",
      dialogFormVisible: false,
      form: {
        age: "",
        name: "",
      },
    };
  },
  methods: {
    addEvt() {
      this.$bus.emit('addEvt',!this.dialogFormVisible)
     
    },
    handleEdit(index, row) {
      this.$bus.emit("editEvt", { index, row, 'tit': "修改信息", dlf: true });
    },
    handleDelete(index, row) {
      this.tableData.splice(index,1)
    },
  },
  watch:{
    tableData:{
      deep:true,
      handler(){
        this.$bus.emit('watchTbData',this.tableData)
      }
    }
  },
  mounted() {
    this.$bus.on('conEvt',res=>{
      this.tableData.push({'date':res.age,'name':res.name})
    }),
    this.$bus.on('editConEvt',res=>{
      this.tableData[res.index].date = res.form.age;
      this.tableData[res.index].name = res.form.name;
    })
  },
  components: {
    AddInfo,
    EditInfo,
  },
  created() {
    axios.get("/api/getList").then((res) => {
      this.tableData = res.data;
    });
  },
};
</script>

<style>
</style>